package com.ds.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ds.api.dao.DSAdvertMapper;
import com.ds.api.dao.TypeMachineMapper;
import com.ds.api.model.DSAdvert;
import com.ds.api.model.TreeModel;
import com.ds.api.model.TypeMachine;
import com.ds.api.utils.JsonUtil;
import com.ds.api.utils.Utils;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;

@RestController
@RequestMapping("/type")
public class TypeMachineController extends BaseController{
    private static final Logger log = LoggerFactory
            .getLogger(TypeMachineController.class);
    @Autowired
    private TypeMachineMapper mapper;

    @Autowired
    private DSAdvertMapper dsmapper;

    @RequestMapping(value = "/query")
    public  void query(@RequestParam Map<String,Object> param,
            HttpServletRequest request, HttpServletResponse response) {
        List<Integer> lists=new ArrayList<Integer>();
        String resultStr="0";
        String fkparentId=param.get("fkParentId")+"";
        if(fkparentId!=null&&!"".equals(fkparentId)) {
            String []fkparentIds=fkparentId.split(",");
            for (String string : fkparentIds) {
                lists.add(Integer.parseInt(string));
            }
        }
        param.put("list", lists);
        Map<String, Object> result = new HashMap<String, Object>();
        List<TypeMachine> list=mapper.query(param);
        //判断用户名是否存在
        result.put("result",resultStr);
        result.put("data",list);
        Gson gson = new Gson();
        try {
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));

        } catch (Exception e) {
            log.info("注册出错："+e);
        }
    }

    @RequestMapping("/get")
    public @ResponseBody String get(@RequestParam Map<String,Object> param,@RequestParam(value="currentPage", required=false) String currentPage){
        List<Integer> lists=new ArrayList<Integer>();
        String fkparentId=param.get("fkParentId")+"";
        if(fkparentId!=null&&!"".equals(fkparentId)) {
            String []fkparentIds=fkparentId.split(",");
            for (String string : fkparentIds) {
                lists.add(Integer.parseInt(string));
            }
        }
        param.put("list", lists);
        List<TypeMachine> list=mapper.query(param);
        Map<String,Object> result=new HashMap<String,Object>();
        PageInfo<TypeMachine> pageInfo = new PageInfo<TypeMachine>(list);
        result.put("data", list);
        return JsonUtil.toJson(result);
    }

    @RequestMapping("/add")
    public @ResponseBody String add(@RequestParam Map<String,String> param,@RequestParam(value="currentPage", required=false) String currentPage){
        String parentId=param.get("parentId");
        String name=param.get("name");
        TypeMachine type=mapper.selectByPrimaryKey(Integer.parseInt(parentId));
        if(name!=null&&!"".equals(name)) {
            String names[]=name.split(",");
            for (String string : names) {
                string=string.trim();
                if(string!=null&&!"".equals(string)) {
                    TypeMachine addtype=new TypeMachine();
                    addtype.setFkParentId(Integer.parseInt(parentId));
                    addtype.setIsDel("0");
                    addtype.setIsLeaf("1");
                    addtype.setLayer((Integer.parseInt(type.getLayer())+1)+"");
                    addtype.setName(string);
                    mapper.insert(addtype);
                }
            }
        }
        String result="0";
        return JsonUtil.toJson(result);
    }

    @RequestMapping(value = "/advert")
    public  void advert(@RequestParam Map<String,Object> param,
            HttpServletRequest request, HttpServletResponse response) {
        List<Integer> lists=new ArrayList<Integer>();
        String resultStr="0";
        String fkparentId=param.get("id")+"";
        if(fkparentId!=null&&!"".equals(fkparentId)&&!"null".equals(fkparentId)) {
            String []fkparentIds=fkparentId.split(",");
            for (String string : fkparentIds) {
                lists.add(Integer.parseInt(string));
            }
        }
        param.put("list", lists);
        Map<String, Object> result = new HashMap<String, Object>();
        List<DSAdvert> list=dsmapper.query(param);
        //判断用户名是否存在
        result.put("result",resultStr);
        result.put("data",list);
        Gson gson = new Gson();
        try {
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));

        } catch (Exception e) {
            log.info("注册出错："+e);
        }
    }
    /**
     * 树状查询
     * @description: TODO
     * @param param
     * @param request
     * @param response   
     * @author xiangwei
     * @date 2017年9月8日
     */
    @RequestMapping("/queryTree")
    public void queryTree(@RequestParam Map<String,Object> param,
            HttpServletRequest request, HttpServletResponse response){
        String resultStr="0";
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> param2 = new HashMap<String, Object>();

        List<Map<String,Object>> childlist=new ArrayList<Map<String,Object>>();

        param.put("id", param.get("fkParentId"));
        
        if("39".equals(param.get("fkParentId"))){
            childlist=Utils.childlist;
        }else{
            Map<String, Object> childrenMap = null;

            Map<String, Object> grandson = null;



            List<Map<String,Object>> lists=mapper.queryTree(param);


            String layer=param.get("layer")+"";
            //封装数据
            for (Map<String,Object> model : lists) {
                param2.clear();

                childrenMap = new  LinkedHashMap<String, Object>();

                param2.put("fkParentId", model.get("value"));
                List<Map<String,Object>> list1=mapper.queryTree(param2);
                if(list1.size()>0){
                    model.put("children", list1);
                    if("3".equals(layer)){
                        for (Map<String, Object> map : list1) {
                            param2.clear();
                            param2.put("fkParentId", map.get("value"));
                            List<Map<String,Object>>  list3=mapper.queryTree(param2);
                            map.put("children", list3);
                        }
                    }
                }
                childlist.add(model);
            }
        }
        result.put("result",resultStr);
        result.put("data",childlist);
        Gson gson = new Gson();
        try {
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(gson.toJson(result));

        } catch (Exception e) {
            log.info("注册出错："+e);
        }  
    }
}
